<template>
  <div class="login">
    <div class="login-wrapper">
      <div class="header">
        <Icon type="log-in"></Icon>
        <span class="title">欢迎登录影子点餐后台编辑系统</span>
      </div>
      <div class="content">
        <Input type="text" v-model="userName" placeholder="请输入用户名">
        <span slot="prepend">
                <Icon type="person"></Icon>
              </span>
        </Input>
        <Input :type="passwordType" v-model="password" placeholder="请输入密码">
        <span slot="prepend">
              <Icon type="locked"></Icon>
            </span>
        <span slot="append"
              @mousedown="displayPassword(true)"
              @mouseup="displayPassword(false)"
        >
            <Icon type="eye"></Icon>
        </span>
        </Input>
        <Button
          type="primary"
          long
          :loading=loadingStatus
          @click="requestLogin"
        >登 录
        </Button>
      </div>
    </div>
  </div>
</template>

<script>
  import {Auth, handleUserInfo} from '../../utils/auth'

  export default {
    data() {
      return {
        userName: '',
        password: '',
        loadingStatus: false,
        passwordType: 'password'
      }
    },
    methods: {
      /*
      * 请求接口 登录
      * */
      requestLogin() {
        let data = {
          username: this.userName,
          password: this.password
        };
        if (!this.checkInput()) {
          return false
        }
        this.loadingStatus = true;
        this.$axios.post(this.$store.getters.getUrl('login'), data).then(res => {
          let result = res.data.data || {};
          this.loadingStatus = false;
          if (res.data.error_code === 0) {
            Auth({auth: result.auth}); // 存储auth信息
            handleUserInfo({userInfo: result.name, type: 'set'}); //存储登录者信息
            setTimeout(() => {
              window.location.reload(); //结合route.js里面的beforeEach 一起工作，保证登录以后跳转至新闻列表页面
            }, 200)
          } else {
            this.$Message.error({
              content: res.data.error_msg
            });
          }
        })
      },
      /*
      * 前端校验 空值
      * */
      checkInput() {
        if (!this.userName || !this.password) {
          this.$Message.error({
            content: '账号或密码为空，请重新输入',
          });
          return false
        } else {
          return true
        }
      },
      /*
      * 显示密码
      * */
      displayPassword(bool) {
        bool ? (this.passwordType = 'text') : (this.passwordType = 'password')
      }
    },
    created() {
    }
  }
</script>

<style lang='less' type='text/less' scoped>
  .login {
    min-width: 1200px;
    height: 100%;
    font-size: 15px;
    background-image: url("../../assets/images/login-bg.jpg");
    background-size: cover;
    .login-wrapper {
      position: relative;
      top: 35%;
      right: 200px;
      float: right;
      width: 400px;
      min-height: 300px;
      border-radius: 8px;
      background: #fff;
      .header {
        height: 50px;
        line-height: 50px;
        padding: 0 24px;
        border-bottom: 1px #ddd solid;
        .title {
          margin-left: 20px;
        }
        .icon {
          vertical-align: middle;
        }
      }
      .content {
        padding: 40px 24px;
        & > * {
          margin-bottom: 25px;
        }
      }
    }
  }
</style>
